package cn.antblog.sort;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author YangJingyi
 * @date 2021-09-17 15:38
 * map 排序
 */
public class SortMap {

    public static void main(String[] args) {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map1 = new HashMap<>();
        map1.put("name", "p");
        map1.put("cj", "5");
        Map<String, String> map2 = new HashMap<>();
        map2.put("name", "h");
        map2.put("cj", "12");
        Map<String, String> map3 = new HashMap<>();
        map3.put("name", "f");
        map3.put("cj", "31");
        Map<String, String> map4 = new HashMap<>();
        map4.put("name", "h");
        map4.put("cj", "45");
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        //排序前
        for (Map<String, String> map : list) {
            System.out.println(map.get("cj"));
        }
        Collections.sort(list,((o1, o2) -> {
            Integer name1 = Integer.valueOf(o1.get("cj"));
            Integer name2 = Integer.valueOf(o2.get("cj"));
            return name1.compareTo(name2);
        }));








//        Collections.sort(list, new Comparator<Map<String, Object>>() {
//            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
//                Integer name1 = Integer.valueOf(o1.get("cj").toString()) ;//name1是从你list里面拿出来的一个
//                Integer name2 = Integer.valueOf(o2.get("cj").toString()) ; //name1是从你list里面拿出来的第二个name
//                return name1.compareTo(name2);
//            }
//        });
        //排序后
        System.out.println("-------------------");
        for (Map<String, String> map : list) {
            System.out.println(map.get("cj"));
        }

    }
}
